<html>
<!--<script type="text/javascript"  src="js/gears_init.js"></script> -->
<script>
var tab_url;

function DepenDNS() {
	var ips;
	var count;
	this.ips = [];
	this.count = 0;
	var count_string = "Load";
	var CenterServer = "http://140.113.87.227/DepenDNS/post.php";
	this.domain = "";
	
	this.GetDomainURL = GetDomainURL;
	function GetDomainURL ( URL )
	{
		var Reg = URL.match(/https{0,1}:\/\/(.[^/]+)/);
		if ( Reg != null ){
			Reg = URL.match(/https{0,1}:\/\/(.[^/]+)/)[1];
			return Reg;
		} else {
			return null;
		}
	}
	
	this.Run = Run;
	function Run(tab) 
	{
		this.domain = this.GetDomainURL(tab.url);
		// not legal URL
		if ( this.domain == null ){
			return false;
		}

		var xmlHttp = new XMLHttpRequest();
		// The browser does not support xmlHttpRequest
		if ( !xmlHttp ){
			count_string = "No";
			chrome.browserAction.setBadgeBackgroundColor( {color:[208, 0, 24, 255]} );
			chrome.browserAction.setIcon({path: "icon/icon_22.png"});
			this.Badge(count_string,tab);
			return false;
		}
		var params = "ASK_URL="+this.domain+"&User=kent&Passwd=123";
		xmlHttp.open( 'POST' , CenterServer , true );
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4){
				if (xmlHttp.status == 200){
					var grade = xmlHttp.responseText.match(/Grade: (\w+)/);
					if (grade != null){
						grade = xmlHttp.responseText.match(/Grade: (\w+)/)[1];
						count_string = grade/10+"";
						chrome.browserAction.setBadgeBackgroundColor( {color:[208, 0, 24, 255]} );
						chrome.browserAction.setIcon({path: "icon/icon_22.png"});
						SetBadge(count_string,tab);
					}
				}
			}
		}
		xmlHttp.send(params);
		chrome.browserAction.setBadgeBackgroundColor( {color:[208, 0, 24, 255]} );
		chrome.browserAction.setIcon({path: "icon/icon_22.png"});
		this.Badge(count_string,tab);
	}
	this.Badge = Badge;
	function Badge( text , tab ) 
	{
		var obj = new Object();
		obj.text = text;
		obj.tabId = tab.id;
		chrome.browserAction.setBadgeText(obj);
	}

}

function SetBadge( text , tab )
{
	var obj = new Object();
	obj.text = text;
	obj.tabId = tab.id;
	chrome.browserAction.setBadgeText(obj);

}

var DepenDNS = new DepenDNS();

// Setup chrome action listener 
chrome.tabs.onSelectionChanged.addListener(function(tabid,selectedInfo){
		chrome.tabs.get(tabid,function(tab) {
			DepenDNS.Run(tab);
			});
		});

chrome.windows.onFocusChanged.addListener(function(windowId) {
		chrome.tabs.getSelected(windowId, function(tab) {
			DepenDNS.Run(tab);
			});
		});

chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
		// make sure the tab hasn't changed...
		chrome.tabs.getSelected(null, function(tab) {
			DepenDNS.Run(tab);
			});
		});

// Message passing between background and popup
var port = chrome.extension.connect({ name: "parse_url"} );
chrome.extension.onConnect.addListener( function(port) {
		console.assert(port.name == "parse_url");	
		port.onMessage.addListener(function(msg) {
			if ( msg.popup == "URL" ){
			//alert(msg.popup);
			port.postMessage( {BackGround: DepenDNS.domain} );
			}
			});
		});

</script>
</html>



